package com.jackchong.utils;

import android.app.Activity;
import android.content.res.Resources;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.jackchong.widget.JEditText;
import com.jackchong.widget.JImageView;
import com.jackchong.widget.JTextView;
import com.utils.R;

import java.util.List;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/24 13:17
 */

public abstract class JRAdapter<T> extends BaseQuickAdapter<T, JRAdapter.ViewHolder> {

    protected Activity mActivity;

    public JRAdapter(int layoutResId, @Nullable List<T> data) {
        super(layoutResId, data);
    }

    protected boolean isAuto() {
        return true;
    }

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        if (isAuto()) JAuto.auto(view);
        if (view.getContext() instanceof Activity) {
            mActivity = (Activity) view.getContext();
        }
        return super.createBaseViewHolder(view);
    }


    public static class ViewHolder extends BaseViewHolder {

        public ViewHolder(View view) {
            super(view);
        }

        public BaseViewHolder setText(@IdRes int viewId, @StringRes int strId) {
            try {
                super.setText(viewId, strId);
            }catch (Resources.NotFoundException e){
                super.setText(viewId, String.valueOf(strId));
            }
            return this;
        }

        public BaseViewHolder setBindTextChange(@IdRes int viewId, JEditText.OnChangedAfterTextListener onChangedAfterTextListener){
            JEditText jEditText = (JEditText) getView(viewId);
            jEditText.setOnChangedAfterTextListener(onChangedAfterTextListener);
            return this;
        }

        public void setSelect(@IdRes int id, boolean select) {
            getView(id).setSelected(select);
        }

        public TextView getTV(@IdRes int id){
            return getView(id);
        }

        public JTextView getJTV(@IdRes int id){
            return getView(id);
        }

        public JImageView getJIV(@IdRes int id){
            return getView(id);
        }


        public String getString(@IdRes int id){
            return getTV(id).getText().toString();
        }

        public ImageView getIV(@IdRes int id){
            return getView(id);
        }

        public void loadImageUrl(@IdRes int id, String url) {
            loadImageUrl(id, url, 0);
        }

        public void loadAvatar(@IdRes int id, String url) {
            loadImageUrl(id, url, R.drawable.default_avatar);
        }

       public void loadImageUrl(@IdRes int id, String url, @DrawableRes int perch) {
            RequestOptions options = new RequestOptions()
                    .error(perch)
                    .placeholder(perch);
            Glide.with(itemView)
                    .load(url)
                    .apply(options)
                    .into(getIV(id));
        }

        public void setOnClickListener(@IdRes int id, View.OnClickListener listener){
            getView(id).setOnClickListener(listener);
        }


    }
}
